package org.dregs.pojo;

import com.google.common.collect.Maps;
import org.dregs.pojo.tuple.Tuple;
import org.dregs.pojo.tuple.Tuple2;
import org.dregs.utils.BuildMap;
import org.dregs.utils.TimeUtil;
import org.springframework.jdbc.core.RowMapper;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

public class Version extends DataBean implements Serializable {

    private Long id;

    private String fileID;

    private String md5;

    private Date time;

    private String url;

    private String description;

    private Integer current;


    public Version() {
    }

    public Version(ResultSet resultSet) throws SQLException {
        this.id = resultSet.getLong("id");
        this.fileID = resultSet.getString("file_id");
        this.md5 = resultSet.getString("md5");
        this.time = resultSet.getTimestamp("time");
        this.url = resultSet.getString("url");
        this.current = resultSet.getInt("current");

    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"id\":")
                .append(id);
        sb.append(",\"fileID\":\"")
                .append(fileID).append('\"');
        sb.append(",\"md5\":\"")
                .append(md5).append('\"');
        sb.append(",\"time\":\"")
                .append(TimeUtil.getString(time,TimeUtil.TIME_FORMAT_STR_yMdhmsS)).append('\"');
        sb.append(",\"url\":\"")
                .append(url).append('\"');
        sb.append(",\"description\":\"")
                .append(description).append('\"');
        sb.append(",\"current\":")
                .append(current);
        sb.append('}');
        return sb.toString();
    }

    public Version(Long id, String fileID, String md5, Date time, String url, String description, Integer current) {

        this.id = id;
        this.fileID = fileID;
        this.md5 = md5;
        this.time = time;
        this.url = url;
        this.description = description;
        this.current = current;
    }
    @Override
    public Map<String,Object> toMap(){
        BuildMap<String, Object> buildMap = new BuildMap<>();
        if(null != id)
            buildMap.put("`id`",id);
        if(null != fileID)
            buildMap.put("`file_id`",fileID);
        if(null != md5)
            buildMap.put("`md5`",md5);
        if(null != time)
            buildMap.put("`time`",time);
        if(null != url)
            buildMap.put("`url`",url);
        if(null != description)
            buildMap.put("`desc`",description);
        if(null != current)
            buildMap.put("`current`",current);
        return buildMap.toMap();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFileID() {
        return fileID;
    }

    public void setFileID(String fileID) {
        this.fileID = fileID;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Tuple2<String,Long> id() {
        return Tuple.initialize("`id`",id);
    }

    public Tuple2<String,String> file_id() {
        return Tuple.initialize("`file_id`",fileID);
    }

    public Tuple2<String,String> md5() {
        return Tuple.initialize("`md5`",md5);
    }

    public Tuple2<String,Date> time() {
        return Tuple.initialize("`time`",time);
    }
    public Tuple2<String,String> desc() {
        return Tuple.initialize("`desc`",description);
    }
    public Tuple2<String,Integer> current() {
        return Tuple.initialize("`current`",current);
    }

}
